package com.example.administrator.capital.model;
import com.example.administrator.capital.R;
import com.example.administrator.capital.model.base.BaseModel;
import com.example.administrator.capital.model.base.IActionRequest;
import com.example.administrator.capital.model.bean.ErrorBean;
import com.example.administrator.capital.url.Url;
import com.example.administrator.capital.utils.JsonUtils;
import com.example.administrator.capital.utils.LogUtils;
import com.example.administrator.capital.utils.PreferenceManager;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

/**
 * Created by Administrator on 2017/12/15 0015.
 *首页model
 *  */
public class HomeModel extends BaseModel {

    /**
     * @param iActionRequest
     * 首页数据
     */
public void homePage(final IActionRequest<String> iActionRequest){
    OkGo.<String>post(Url.HOMEPAGE).tag(this).execute(new StringCallback() {
        @Override
        public void onSuccess(Response<String> response) {
            LogUtils.d("首页数据"+response.body());
            if (response.body().contains("\"code\":1")) {
                iActionRequest.success(response);
            } else {
                ErrorBean bean = JsonUtils.parse(response.body(), ErrorBean.class);

                    iActionRequest.failMsg(bean.getMsg());



            }
        }
        @Override
        public String convertResponse(okhttp3.Response response) throws Throwable {
            return super.convertResponse(response);
        }
        @Override
        public void onError(Response<String> response) {
            super.onError(response);
            iActionRequest.code(R.string.error_msg);
        }
    });
}
    /**
     * @param iActionRequest
     * @param uid
     * 查询我的邀请接口
     */
    public void selective(final IActionRequest<String> iActionRequest,
                          String uid){

        OkGo.<String>post(Url.SELECTIVE).tag(this).headers("TOKEN", PreferenceManager.instance().getToken()).params("uid",uid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtils.d("查询我的邀请"+response.body());
                        if (response.body().contains("\"code\":1")) {
                            iActionRequest.success(response);
                        } else {
                            ErrorBean bean = JsonUtils.parse(response.body(), ErrorBean.class);
                            iActionRequest.failMsg(bean.getMsg());
                        }
                    }
                    @Override
                    public String convertResponse(okhttp3.Response response) throws Throwable {
                        return super.convertResponse(response);
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        iActionRequest.code(R.string.error_msg);
                    }
                });
    }

}
